<!--
 * @FilePath: /web-vue/src/views/ScenerySpot/editForm.vue
 * @Description: 【旅游景点】编辑表单
-->
<template>
  <div>
    <!-- 弹出层-表单 -->
    <el-dialog
      class="edit-dialog"
      title="编辑【旅游景点】"
      :visible.sync="visible"
      :confirmLoading="confirmLoading"
      top="7vh"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      :fullscreen="true"
    >
      <el-form
        :model="editForm"
        :rules="rules"
        ref="editForm"
        status-icon
        label-width="150px"
        style="width: 100%"
        class="layer-form-style"
      >
        <el-form-item label="ID：" prop="id" size="mini">
          <el-input v-model="editForm.id" ID readOnly></el-input>
        </el-form-item>
        <el-form-item label="景点名称：" prop="name" size="mini">
          <el-input v-model="editForm.name" placeholder="请输入景点名称" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="景点类型：" prop="sceneryType" size="mini">
          <el-select
            v-model="editForm.sceneryType"
            filterable
            clearable
            placeholder="请选择景点类型"
            style="width:99%"
          >
            <el-option
              v-for="item in $store.state.DICT.sceneryTypeSelect"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="景点图：" prop="sceneryImageUrl" size="mini">
          <el-input
            v-model="editForm.sceneryImageUrl"
            placeholder="景点图图片上传回调路径"
            readOnly
            style="width: 99%"
          ></el-input>
          <el-image
            v-if="imageVisible"
            style="width: 100px; height: 100px"
            :src="`${$store.state.API_ROOT}${editForm.sceneryImageUrl}`"
          >
          </el-image>
          <!-- 图片上传 -->
          <UploadImage
            ref="uploadImage"
            v-bind:dialogImageUrl="editForm.sceneryImageUrl"
            @afterUpload="(val) => (editForm.sceneryImageUrl = val)"
          ></UploadImage>
        </el-form-item>
        <el-form-item label="宣传标语：" prop="announceTitle" size="mini">
          <el-input v-model="editForm.announceTitle" placeholder="请输入宣传标语" clearable style="width:99%"></el-input>
        </el-form-item>
        <el-form-item label="详情内容：" prop="detailArticle" size="mini">
          <ckeditor v-model="editForm.detailArticle" style="width: 99%"></ckeditor>
        </el-form-item>
        <el-form-item label="景点状态：" prop="sceneryStatus" size="mini">
          <el-select
            v-model="editForm.sceneryStatus"
            filterable
            clearable
            placeholder="请选择景点状态"
            style="width:99%"
          >
            <el-option
              v-for="item in $store.state.DICT.sceneryStatusSelect"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-finished"
          @click="handleSubmit()"
          >保存
        </el-button>
        <el-button icon="el-icon-refresh-left" @click="handleCancel()"
          >取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UploadImage from "@/components/Upload/uploadImage";
import { DateTimeRange_Picker_Option, DateRange_Picker_Option, Date_Picker_Option } from "@/utils/datePicker";

export default {
  components: {
    UploadImage,
  },

  data() {
    return {
      visible: false,
      confirmLoading: false,
      editForm: {
        id: null,
        name: null,
        sceneryType: null,
        sceneryImageUrl: null,
        announceTitle: null,
        detailArticle: null,
        sceneryStatus: null,
      // 辅助属性
      },
      // 表单校验规则
      rules: {
        id: [
          {
            required: true,
            message: "ID不能为空！",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "景点名称不能为空！",
            trigger: "blur",
          },
        ],
        sceneryImageUrl: [
          {
            required: true,
            message: "景点图不能为空！",
            trigger: "blur",
          },
        ],
        announceTitle: [
          {
            required: true,
            message: "宣传标语不能为空！",
            trigger: "blur",
          },
        ],
        sceneryStatus: [
          {
            required: true,
            message: "景点状态不能为空！",
            trigger: "blur",
          },
        ],
      },
      // 日期选择快捷方式
      DateTimeRange_Picker_Option,
      DateRange_Picker_Option,
      Date_Picker_Option,
      // Image 可视?
      imageVisible: false,
    };
  },

  methods: {
    // 打开【旅游景点】表单编辑窗口
    openEditForm(isShow, item) {
      this.visible = isShow;
      // 【旅游景点】编辑信息填充（延迟执行，确保子组件初始化）
      setTimeout(() => {
        // 清除表单校验结果
        this.$refs["editForm"].clearValidate();
        // 赋值
        this.editForm.id = item.id;
        this.editForm.name = item.name;
        this.editForm.sceneryType = item.sceneryType;
        this.editForm.sceneryImageUrl = item.sceneryImageUrl;
        this.editForm.announceTitle = item.announceTitle;
        this.editForm.detailArticle = item.detailArticle;
        this.editForm.sceneryStatus = item.sceneryStatus;
        // 图片显示
        this.imageVisible = true;
      }, 1);
    },

    // 关闭【旅游景点】表单编辑窗口
    handleCancel() {
      this.$refs["editForm"].resetFields();
      this.visible = false;
      // 图片显示
      this.imageVisible = false;

    // 清空上传文件列表
      this.$refs["uploadImage"].clearUploadedFileList();
    },

    // 提交
    handleSubmit() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            target: document.querySelector(".edit-dialog"),
            lock: true,
            text: "正在保存，请耐心等待！",
          });

          // 请求参数
          let param = Object.assign({}, this.editForm);



          // 发起请求
          this.$api
            .modifyScenerySpot(param)
            .then((res) => {
              loading.close();
              let { status, message, data } = res.data; // 响应数据
              if (status == 200) {
                this.$emit("afterSubmit");
                this.handleCancel();
                // 提示信息
                this.$message({
                  type: "success",
                  message: `操作成功！已编辑【旅游景点】!${message}`,
                  duration: 1680,
                });
              } else if (status == 403) {
                this.$message({
                  message: `权限不足！${message}`,
                  type: "error",
                  center: true,
                  duration: 1680,
                });
                // this.$router.push("/login");
              } else {
                this.$alert(`操作失败！${message}`, "【旅游景点】编辑", {
                  type: "error",
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }
            })
            .catch((err) => {
              loading.close();
              this.visible = false;
              console.log(`【旅游景点】编辑接口发生错误：${err}`);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
// 表单布局样式
@import "@/assets/css/form-layout.scss";
</style>
